#ifndef __UTILITY_H__
#define __UTILITY_H__

#include <ctime>
#include <iostream>
#include <string>
#include <sys/stat.h>

namespace log_system
{
    namespace utility
    {
        class Date
        {
        public:
            static size_t getTime() // 获取当前时间
            {
                return (size_t)time(nullptr);
            }

        }; // class Date

        class File
        {
        public:
            static bool isExists(const std::string &path_name) // 文件是否存在
            {
                struct stat st;
                if (stat(path_name.c_str(), &st) == -1)
                    return false;
                else
                    return true;
            }

            static std::string getPath(const std::string &path_name) // 获取文件路径
            {
                size_t position = path_name.find_last_of("/\\");
                if (position == std::string::npos)
                    return ".";
                return path_name.substr(0, position + 1);
            }
            
            static void createDirectory(const std::string &path_name) // 创建目录
            {
                size_t position = 0, index = 0;
                while (index < path_name.size())
                {
                    position = path_name.find_first_of("/\\", index);
                    if (position == std::string::npos)
                    {
                        mkdir(path_name.c_str(), 0777);
                        break;
                    }
                    std::string parent_dir = path_name.substr(0, position + 1);
                    if (isExists(parent_dir) == true)
                    {
                        index = position + 1;
                        continue;
                    }
                    else
                    {
                        mkdir(parent_dir.c_str(), 0777);
                        index = position + 1;
                    }
                }
            }
        };
    } // namespace utility
} // namespace

#endif // ifndef UTILITY_H
